﻿using Domain.Process_Procedures;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.ProcessCategory;
using MES.Server.API.Dto.ProcessProceduresDTO;
using Microsoft.EntityFrameworkCore;
using ReturnValue;

namespace MES.Server.API.Application.Handler.ProcessCategory
{
    public class GetProcessCategoryHandler : IRequestHandler<GetProcessCategoryCommand, ApiPaging<ProcessCategoryDTO>>
    {
        private readonly IRepository<ProcessCategoryModel> categoryRepo;

        public GetProcessCategoryHandler(IRepository<ProcessCategoryModel> CategoryRepo)
        {
            categoryRepo = CategoryRepo;
        }
        public async Task<ApiPaging<ProcessCategoryDTO>> Handle(GetProcessCategoryCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiPaging<ProcessCategoryDTO>();
            var categoryRepos=categoryRepo.GetAll().Where(x=>!x.IsDel);
            var linq = from a in categoryRepos
                       select new ProcessCategoryDTO()
                       {
                           Id = a.Id,
                           CategoryCode=a.CategoryCode,
                           CategoryName=a.CategoryName,
                           SortOrder = a.SortOrder,
                           IsActive=a.IsActive,
                           Remarks = a.Remarks
                       };
            res.Totalcount= await linq.CountAsync();
            res.TotalPage = await linq.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToListAsync();
            res.Message = "查询成功";
            res.Code = StatusCode.Success;
            return res;
        }
    }
}
